/*
 * Copyright 2008. Mount Sinai Hospital, Toronto, Canada.
 * 
 * Licensed under the Apache License, Version 2.0. You
 * can find a copy of the license at:
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * IN NO EVENT SHALL MOUNT SINAI HOSPITAL BE LIABLE TO ANY PARTY FOR DIRECT, 
 * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST 
 * PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, 
 * EVEN IF MOUNT SINAI HOSPITAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
 * DAMAGE.
 * 
 * MOUNT SINAI HOSPITAL SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND 
 * ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". 
 * MOUNT SINAI HOSPITAL HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, 
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 
 */

package com.sinai.mshab.visibility.rule;

import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;

import com.sinai.mshab.visibility.VisibilityRule;

import java.util.HashSet;
import java.util.Set;

/**
 * The <code>ShowIfSelected</code> {@link VisibilityRule} is a rule that allows
 * a child widget to be shown when a particular {@link ListBox} widget element
 * is selected.
 * 
 * @author Arthur Kalmenson
 */
public class ShowIfSelected implements VisibilityRule<ListBox, Widget, String> {

	/**
	 * The parent {@link ListBox} widget.
	 */
	private ListBox parent;

	/**
	 * The child widget.
	 */
	private Widget child;

	/**
	 * The set of triggers that would make the child widget appear.
	 */
	private Set<String> triggers = new HashSet<String>();

	/**
	 * Creates a new <code>ShowIfSelected</code> with the given parent
	 * {@link ListBox}, trigger value and child widget.
	 * 
	 * @param parent
	 *            the parent {@link ListBox}.
	 * @param trigger
	 *            the trigger value.
	 * @param child
	 *            the child widget.
	 */
	public ShowIfSelected(ListBox parent, String trigger, Widget child) {
		if (parent == null || trigger == null || child == null) {
			throw new IllegalArgumentException("All constructor arguments to "
					+ getClass().getName() + " must be instantiated.");
		}
		this.parent = parent;
		this.child = child;
		triggers.add(trigger);

		// add the change listener to the parent.
		parent.addChangeListener(new ListBoxSelectListener());
	}

	public void execute() {
		int selectedIndex = parent.getSelectedIndex();
		if (selectedIndex != -1
				&& triggers.contains(parent.getValue(selectedIndex))) {
			child.setVisible(true);
		} else {
			child.setVisible(false);
		}
	}

	public void setParentWidget(ListBox parent) {
		if (parent == null) {
			throw new IllegalArgumentException(getClass().getName()
					+ ": Parent widget cannot be null.");
		}
		this.parent = parent;

		// add the change listener to the new parent.
		parent.addChangeListener(new ListBoxSelectListener());
	}

	public ListBox getParentWidget() {
		return parent;
	}

	public void setChildWidget(Widget child) {
		if (child == null) {
			throw new IllegalArgumentException(getClass().getName()
					+ ": Child widget cannot be null.");
		}
		this.child = child;
	}

	public Widget getChildWidget() {
		return child;
	}

	public void addTrigger(String value) {
		if (value == null) {
			throw new IllegalArgumentException(getClass().getName()
					+ ": Trigger cannot be null.");
		}
		triggers.add(value);
	}

	public Set<String> getTriggers() {
		return triggers;
	}

	/**
	 * The {@link ChangeListener} that listens for items being selected in the
	 * list box and executes the rule when they are.
	 * 
	 * @author Arthur Kalmenson
	 */
	private class ListBoxSelectListener implements ChangeListener {

		public void onChange(Widget arg0) {
			execute();
		}
	}
}
